package codeassist.ast;

import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

/**
 * 遍历ASTNode树形结构,会递归调用,vist(this)总会访问到每一个节点,在visit方法里面处理具体的访问逻辑
 */
public class DemoVisitor extends ASTVisitor {

	@Override
	public boolean visit(FieldDeclaration node) {
		for (Object obj : node.fragments()) {
			VariableDeclarationFragment v = (VariableDeclarationFragment) obj;
			System.out.println("Field:\t" + v.getName());
		}

		return true;
	}

	@Override
	public boolean visit(MethodDeclaration node) {
		System.out.println("Method:\t" + node.getName());
		Javadoc javadoc = node.getJavadoc();//对应一个 /**javadoc*/
		if(null != javadoc){
			List<TagElement> tags = javadoc.tags();//注释和return分为两个TagElement
			for(TagElement o : tags){
				//注释的TagElement name为null
				//return的TagElement name 为@return
				
				List fragments = o.fragments();
				for(Object te : fragments){
					//有的为simpleName
					//注释每一行为一个TextElement, TextElement的值Text为具体注释值
					System.out.println("注释:\t" + te.toString());
					
				}
				
			}
		}
		return true;
	}

	@Override
	public boolean visit(TypeDeclaration node) {
		System.out.println("Class:\t" + node.getName());
		return true;
	}
}
